import 'dart:io' as io;
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:super_tool_server/business/config_manager.dart';
import 'package:super_tool_server/business/log_manager.dart';
import 'package:super_tool_server/common/utils/log.dart';
import 'package:super_tool_server/component/button/round_button.dart';
import 'package:super_tool_server/model/config_info.dart';
import 'package:super_tool_server/model/log_item_info.dart';
import 'dart:convert';

class LogPanelWidget extends StatelessWidget {
  LogPanelWidget({Key? key, this.autoScrollLog = true}) : super(key: key);

  final bool autoScrollLog;
  final ScrollController controller = ScrollController();

  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
        stream: LogManager.getInstance().stream,
        initialData: LogManager.getInstance().getLogList(),
        builder: (context, snapshot) {
          List<LogItemInfo>? logList = snapshot.data;
          if (autoScrollLog) {
            Future.delayed(Duration(milliseconds: 16)).then((value) => controller.animateTo(
                controller.position.maxScrollExtent,
                duration: Duration(milliseconds: 200),
                curve: Curves.easeOutQuart));
          }
          return Container(
            alignment: Alignment.center,
            color: Colors.black.withOpacity(0.9),
            child: ListView.builder(
                controller: controller,
                padding: EdgeInsets.symmetric(horizontal: 20, vertical: 20),
                itemCount: logList!.length,
                itemBuilder: (BuildContext context, int index) {
                  return Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      index > 0
                          ? Container(
                        margin: EdgeInsets.symmetric(vertical: 8.0),
                        color: Colors.grey,
                        height: 1.0,
                      )
                          : Container(),
                      SelectableText(
                        logList[index].title,
                        style: TextStyle(fontSize: 18, color: logList[index].color),
                      ),
                    ],
                  );
                }),
          );
        });
  }
}
